﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lr.Core.Validation.Fluent
{
    /// <summary>
    /// Helps building an <see cref="IValidationRule"/>.
    /// </summary>
    /// <typeparam name="TValidatable">The type of the <see cref="IValidatable"/>.</typeparam>
    public class ValidationRuleBuilderWithErrorMessage<TValidatable>
        where TValidatable : IValidatable
    {
        /// <summary>
        /// Gets the <see cref="IValidatable"/> that started the process.
        /// </summary>
        internal IValidatable Validatable { get { return _validatable; } }
        private readonly IValidatable _validatable;

        /// <summary>
        /// Gets the error message to be displayed when the rule fails.
        /// </summary>
        internal string ErrorMessage { get { return _errorMessage; } }
        private readonly string _errorMessage;

        /// <summary>
        /// Creates a new <see cref="ValidationRuleBuilderWithErrorMessage"/> of <typeparamref name="TValidatable"/> object.
        /// </summary>        
        /// <param name="parent">The parent object from which this object was created.</param>
        /// <param name="errorMessage">The error message to be displayed when the rule fails.</param>
        internal ValidationRuleBuilderWithErrorMessage(ValidationRuleBuilder<TValidatable> parent, string errorMessage)
        {
            ThrowIf.Null(() => parent);
            ThrowIf.NullOrEmpty(() => errorMessage);

            _validatable = parent.Validatable;
            _errorMessage = errorMessage;
        }

        /// <summary>
        /// Continues the building process for a specific <typeparamref name="TValidatable"/>.
        /// </summary>
        /// <param name="ruleAlgorithm">The rule's algorithm that determines whether a <typeparamref name="TValidatable"/> fails the rule or not.</param>
        /// <returns>A <see cref="ValidationBuilderWithRuleAlgorithm"/> of type <typeparamref name="TValidatable"/>.</returns>
        public ValidationRuleBuilderWithRuleAlgorithm<TValidatable> AndRuleAlgorithm(Func<TValidatable, bool> ruleAlgorithm)
        {
            return new ValidationRuleBuilderWithRuleAlgorithm<TValidatable>(this, ruleAlgorithm);
        }

        /// <summary>
        /// Returns a <see cref="String"/> that represents the object.
        /// </summary>
        /// <returns>A <see cref="String"/> that represents the object.</returns>
        public override string ToString()
        {
            return this.ToDebugString();
        }
    }
}
